﻿using AutoMapper;
using MediatR;
using SU.Center.Domain.SY;
using SU.Center.Infa;

namespace SU.Center.Api.Handler.TraceabilityMethod
{
    /// <summary>
    /// 显示产品
    /// </summary>
    public class GetInformationHandler : IRequestHandler<GetInformationReqsultDto, ApiResult>
    {
        private readonly IBaseReposiotry<Information> baseReposiotry;
        private readonly IBaseReposiotry<Product> reposiotry;
        private readonly IMapper mapper;
        private readonly ILogger<GetInformationHandler> logger;

        public GetInformationHandler(IBaseReposiotry<Information> baseReposiotry, IBaseReposiotry<Product> reposiotry, IMapper mapper, ILogger<GetInformationHandler> logger)
        {
            this.baseReposiotry = baseReposiotry;
            this.reposiotry = reposiotry;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult> Handle(GetInformationReqsultDto request, CancellationToken cancellationToken)
        {
            var att = await baseReposiotry.GetsAll(x => x.InformationDel == false);
            var user = await reposiotry.GetAll();

            var addr = from a in att
                       join b in user on a.ProductId equals b.ProductId
                       select new GetInformationResponseDto
                       {
                           InformationBatch = a.InformationBatch,
                           InformationDay = a.InformationDay,
                           InformationCertificate = a.InformationCertificate,
                           InformationEnterprise = a.InformationEnterprise,
                           InformationImprove = a.InformationImprove,
                           InformationCreate = a.InformationCreate,
                           InformationTiem = a.InformationTiem,
                           InformationModify = a.InformationModify,
                           InformationCover = a.InformationCover,
                           ProductId = a.ProductId,
                           InformationId = a.InformationId,
                           ProductName = b.ProductName
                       };


            var reout = new PagResult
            {
                toCount = addr.Count(),
                toIndex = Math.Ceiling(addr.Count() / (double)request.Size),
                List = addr.Skip((request.Index - 1) * request.Size).Take(request.Size).ToList()
            };
            return await Task.FromResult(ApiResult.Ok(reout));
        }
    }
}
public class GetInformationReqsultDto : IRequest<ApiResult>
{
    public int Index { get; set; }
    public int Size { get; set; }
}

public class GetInformationResponseDto
{
    /// <summary>
    /// 主键
    /// </summary>
    public int InformationId { get; set; }
    /// <summary>
    /// 生产批次
    /// </summary>
    public string InformationBatch { get; set; }
    /// <summary>
    /// 保质期（天）
    /// </summary>
    public string InformationDay { get; set; }
    /// <summary>
    /// 生产许可证号
    /// </summary>
    public string InformationCertificate { get; set; }
    /// <summary>
    /// 生产企业
    /// </summary>
    public string InformationEnterprise { get; set; }
    /// <summary>
    /// 信息完善度
    /// </summary>
    public string InformationImprove { get; set; }
    /// <summary>
    /// 创建人
    /// </summary>
    public string InformationCreate { get; set; }
    /// <summary>
    /// 创建时间
    /// </summary>
    public string InformationTiem { get; set; }
    /// <summary>
    /// 修改人
    /// </summary>
    public string InformationModify { get; set; }
    /// <summary>
    /// 封面
    /// </summary>
    public string InformationCover { get; set; }

    public int ProductId { get; set; }
    public string ProductName { get; set; }
}